﻿using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using Mido.Domain;
using Mido.UiModules.SettingsModule;
using Mido.Utils.Mvvm;
using System;
using System.Collections.ObjectModel;

namespace Mido.UiModules.ScrollBarModule
{
    public class ScrollBarViewModel : ViewModelBase
    {
        private readonly ScrollBarModel _model;
		private readonly ObservableCollection<ProcessedSmallImageViewModel> _internalCollection = new ObservableCollection<ProcessedSmallImageViewModel>();

	    private bool _isImagesLoading;
	    private double _imageLoadingPercents;

        internal ScrollBarViewModel(ScrollBarModel model)
        {
            if (model == null) 
                throw new ArgumentNullException("model");

            _model = model;
			_model.SelectedItemChanged += OnSelectedItemChanged;
			_model.Images.CollectionChanged += OnImagesCollectionChanged;

			for (int index = 0; index < _model.Images.Count;index++)
			{
				ProcessedImage image = _model.Images[index];
				ProcessedSmallImageViewModel viewModel = new ProcessedSmallImageViewModel(image);
				_internalCollection.Add(viewModel);
			}
        }

		private void OnImagesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			if (e.Action == NotifyCollectionChangedAction.Add)
			{
				for (int index = 0; index < e.NewItems.Count; index++)
				{
					ProcessedImage item = (ProcessedImage) e.NewItems[index];
					_internalCollection.Insert(e.NewStartingIndex + index, new ProcessedSmallImageViewModel(item));
				}
			}
			else if (e.Action == NotifyCollectionChangedAction.Remove)
			{
				for (int index = e.OldItems.Count - 1; index >= 0; index--)
				{
					_internalCollection.RemoveAt(e.OldStartingIndex + index);
				}
			}
			else Debug.Fail("There is unknown event type.");
		}

	    public Visibility ProgressVisibility
	    {
		    get { return _isImagesLoading ? Visibility.Visible : Visibility.Collapsed; }
	    }

	    public double ProgressPercents
	    {
		    get { return _imageLoadingPercents; }
	    }

		internal void SetProgressBar(bool isLoading, double progress)
		{
			if (_isImagesLoading != isLoading)
			{
				_isImagesLoading = isLoading;
				RaisePropertyChanged("ProgressVisibility");
			}

			_imageLoadingPercents = progress;
			RaisePropertyChanged("ProgressPercents");
		}

		private void OnSelectedItemChanged()
		{
			RaisePropertyChanged("SelectedImage");
		}

		public ProcessedSmallImageViewModel SelectedImage
	    {
			get { return _model.ActiveImage == null ? null : _internalCollection.First(x => x.Source == _model.ActiveImage); }
			set { _model.ActiveImage = value.Source; }
	    }

        public ObservableCollection<ProcessedSmallImageViewModel> Images
        {
            get { return _internalCollection; }
        }

	    public static ScrollBarViewModel Designer
	    {
		    get
		    {
				ProcessedImage[] images = new []{ new ProcessedImage(string.Empty, new SettingsModel()) };
			    ScrollBarModel model = new ScrollBarModel(new ObservableCollection<ProcessedImage>(images));
			    return new ScrollBarViewModel(model);
		    }
	    }
    }
}
